/*
 * To change this license header, choose License Headers in Project Properties. To change this
 * template file, choose Tools | Templates and open the template in the editor.
 */
package chalmers.game.subterranean.model.player;

import chalmers.game.subterranean.model.physics.IMovableBody;
import chalmers.game.subterranean.util.Direction;
import chalmers.game.subterranean.util.options.Speeds;
import lombok.Setter;
import org.apache.log4j.Logger;

/**
 * 
 * @author Alex Evert
 */
public class StateAir implements IState {
  private static final Logger logger = Logger.getLogger("subLog");

  private @Setter
  PlayerContext player;
  private @Setter
  double landingSpeedSign;

  @Override
  public void jump(IMovableBody body) {}

  @Override
  public void run(IMovableBody body, Direction direction) {
    // The player is allowed some air control.
    // We try to avoid abrupt changes to speed.
    if (direction == Direction.EAST) {
      body.setXVelocity(Math.max(Speeds.AIRSPEED, body.getXVelocity() - 1));
    } else if (direction == Direction.WEST) {
      body.setXVelocity(Math.min(-Speeds.AIRSPEED, body.getXVelocity() + 1));
    }
  }

  @Override
  public void stop(IMovableBody body) {}

  @Override
  public void bounce(IMovableBody body, Direction direction) {
    switch (direction) {
      case NORTH:
        body.setYVelocity(0);
        break;
      case SOUTH:
        // The player lands.
        body.setYVelocity(0);
        body.setXVelocity(landingSpeedSign * Speeds.RUNSPEED);
        player.setStateGround();
        break;
      case EAST:
        body.setXVelocity(Speeds.AIRSPEED);
        body.setYVelocity(body.getYVelocity() * 0.1);
        player.setStateWall(direction);
        logger.info("The player is clinging to wall.");
        break;
      case WEST:
        body.setXVelocity(-Speeds.AIRSPEED);
        body.setYVelocity(body.getYVelocity() * 0.1);
        player.setStateWall(direction);
        logger.info("The player is clinging to wall.");
        break;
    }
  }

  @Override
  public void checkSurrounding(IMovableBody body) {}
}
